﻿using HuiZe.education.Model;
using HuiZe.education.Resource.Controls;
using HuiZe.education.ViewModel;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace HuiZe.education.View
{
    /// <summary>
    /// CloudClassRoom.xaml 的交互逻辑
    /// </summary>
    public partial class CloudClassRoomView : HuiZeWindow
    {
        public CloudClassRoomView()
        {
            InitializeComponent();
            Loaded += LoginView_Loaded;
        }

        void LoginView_Loaded(object sender, RoutedEventArgs e)
        {
            _current_id = -1;
            _is_loaded_first = false;
            pageManage.LoadDataCallback = LoadResource;
            AttendClassViewModel CloudClassModel = DataContext as AttendClassViewModel;
            _cloudList = CloudClassModel.Getcloudclass();
            PART_CoursesCloudList.ItemsSource = _cloudList;
        }

        void PART_CoursesItem_Cloud_Loaded(object sender, RoutedEventArgs e)
        {
            var _rb = (RadioButton)sender;
            _rb.Checked += PART_CoursesItem_Cloud_Checked;

            if (!_is_loaded_first)
            {
                _is_loaded_first = true;
                _rb.IsChecked = new bool?(true);
            }
        }

        void PART_CoursesItem_Cloud_Checked(object sender, RoutedEventArgs e)
        {
            CloudClassModel = DataContext as AttendClassViewModel;
            ResCloudclassModel _model = (ResCloudclassModel)((RadioButton)sender).DataContext;
            _current_id = _model.id;
            LoadResource(1);
            pageManage.ItemSourceCount = CloudClassModel.cloudPageCount;
            pageManage.UpdataPages();
        }

        void PART_Cloud_Resource_Item_Loaded(object sender, RoutedEventArgs e)
        {
            PART_Clound_LessionItem _item = (PART_Clound_LessionItem)sender;
            _item.OpenResourceCallback = OpenResource;
        }

        void OpenResource(RescloudrRoomItemModel _res_item)
        {
            if (_res_item == null || string.IsNullOrEmpty(_res_item.file_path)) return;

            Win_CloudClass_ResourceViewer _win = new Win_CloudClass_ResourceViewer(_res_item.name, _res_item.file_path);
            _win.Owner = this;
            _win.ShowDialog();
        }

        void LoadResource(int _page_index)
        {
            _list_room_cloud = CloudClassModel.GetcloudRoom(_current_id, _page_index);
            PART_LessionItems.ItemsSource = _list_room_cloud;
        }

        int _current_id;
        bool _is_loaded_first;
        AttendClassViewModel CloudClassModel;
        List<RescloudrRoomItemModel> _list_room_cloud;
        public List<ResCloudclassModel> _cloudList = null;
    }
}
